/*
 * author      : 吴中勤
 * date        : 2018/8/22
 * description : 物料转换 sap -> moby
 */

package com.deepexi.adapter.rest.sap.parser;

import com.deepexi.adapter.facade.sap.request.SapPushMaterialBody;
import com.deepexi.adapter.facade.sap.request.SapPushMaterialBody.ShopSwitch;
import com.deepexi.adapter.facade.sap.request.SapPushMaterialBody.Warehouse;
import com.deepexi.adapter.sdk.moby.request.Material;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.*;

public class SapPushMaterialParser extends BaseParser<Material, SapPushMaterialBody> {

    private static final String SAP_MATERIAL_AVAILABLE = "Y";
    private static final String Moby_MATERIAL_AVAILABLE = "1";
    private static final String MOBY_MATERIAL_UNAVAILABLE = "0";

    @Override
    protected void convertEachSapItem(SapPushMaterialBody sapItem) {
        List<Warehouse> warehouseList = sapItem.getWarehouseList();
        List<ShopSwitch> shopSwitchList = sapItem.getShopSwitchList();

        if (null == shopSwitchList || 0 == shopSwitchList.size()) {
            logger.warn("物料没有定义销售组织");
            return;
        }

        /* 一个物料, 多个门店, 每个门店多个仓位 */
        Map<String, ShopSwitch> shopSwitchCache = new HashMap<>();
        shopSwitchList.forEach(shopSwitch -> {
            shopSwitchCache.put(shopSwitch.getShopCode(), shopSwitch);
        });

        if (null == warehouseList || 0 == warehouseList.size()) {
            /* 只有 ShopSwitch */
            logger.info("物料推送, 仅有门店开关");
            for (ShopSwitch aShop : shopSwitchList) {
                Material mobyItem = fromShopSwitch(sapItem, aShop, null);
                mobyRequestBodyList.add(mobyItem);
            }
        } else {
            /* 有仓位开关, 仓位一定有店铺信息 */

            /* 已经处理的门店开关 */
            Set<String> shopSet = new HashSet<>(shopSwitchList.size());

            for (Warehouse wh : warehouseList) {
                ShopSwitch shopSwitch = shopSwitchCache.get(wh.getShopCode());
                Material mobyItem = fromShopSwitch(sapItem, shopSwitch, wh);
                shopSet.add(wh.getShopCode());
                /* */
                mobyRequestBodyList.add(mobyItem);
            }

            /* 未处理的门店 */
            for (ShopSwitch aShop : shopSwitchList) {
                if (!shopSet.contains(aShop.getShopCode())) {
                    logger.warn("物料推送, 仓库信息不能覆盖 销售组织, shopCode={}", aShop.getShopCode());
                    Material mobyItem = fromShopSwitch(sapItem, aShop, null);
                    mobyRequestBodyList.add(mobyItem);
                }
            }
        }
    }

    @Override
    protected List<SapPushMaterialBody> parseSapListFromRecordsStr(String recordsStr) {
        Type collectionType = new TypeToken<List<SapPushMaterialBody>>() {
        }.getType();
        return new Gson().fromJson(recordsStr, collectionType);
    }

    private String availableConvert(String sapAvailableValue) {
        return SAP_MATERIAL_AVAILABLE.equals(sapAvailableValue)
                ? Moby_MATERIAL_AVAILABLE : MOBY_MATERIAL_UNAVAILABLE;
    }

    private Material fromShopSwitch(SapPushMaterialBody sapItem, ShopSwitch aShop, Warehouse wh) {
        Material mobyItem = new Material();
        mobyItem.setName(sapItem.getMaterialName());
        mobyItem.setCode(sapItem.getMaterialCode());
        mobyItem.setExternalCode(sapItem.getExternalMaterialCode());
        mobyItem.setMaterialGroup(sapItem.getMaterialGroup());
        mobyItem.setExternalGroupCode(sapItem.getExternalMaterialGroup());

        /* 门店 */
        if (null != aShop) {
            mobyItem.setShopCode(aShop.getShopCode());
            mobyItem.setShopAvailable(availableConvert(aShop.getShopAvailable()));
        }

        /* 仓库 */
        if (null != wh) {
            mobyItem.setWarehouseCode(wh.getWarehouseCode());
            mobyItem.setWarehouseCodeAvailable(availableConvert(wh.getWarehouseAvailable()));
        }

        return mobyItem;
    }
}
